#pragma once

#include <iostream>
#include <string>
#include <unordered_map>
#include <assert.h>
#include <vector>
#include <fstream>

#include "../comm/log.hpp"
#include "../comm/util.hpp"

namespace ns_Model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;
    struct Question
    {
        string num;
        string title;
        string star;
        int cpu_limit;
        int mem_limit;
        string desc;   //对题目的描述及内容
        string header; //题目的预设代码
        string tail;   //题目的测试代码
    };
    const string question_list = "./questions/questions.list";
    const string question_path = "./questions/";

    class Model
    {
    private:
        //通过题号来建立哈希映射
        unordered_map<string, Question> questions;

    public:
        Model()
        {
            assert(LoadQuestionList(question_list));
        }
        //加载所有的文件到内存
        bool LoadQuestionList(const string &question_list)
        {
            ifstream in(question_list);
            if (!in.is_open())
            {
                LOG(FATAL) << "加载题库失败，请检查是否存在题库文件" << endl;
                return false;
            }
            string line;
            while (getline(in, line))
            {
                vector<string> tokens;
                StringUtil::SplitString(line, &tokens, " ");
                if (tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败,请对应文件中的题目格式" << endl;
                    continue;
                }
                // 1 判断回文数 简单 1 30000
                Question q;
                q.num = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());

                string q_num_path = question_path;
                q_num_path += q.num;
                FileUtil::ReadFile(q_num_path + "/desc.txt", &q.desc, true);
                FileUtil::ReadFile(q_num_path + "/header.cpp", &q.header, true);
                FileUtil::ReadFile(q_num_path + "/tail.cpp", &q.tail, true);

                questions.insert(make_pair(q.num, q));
            }
            LOG(INFO) << "加载题库.....成功" << endl;
            in.close(); //永远不要忘记文件打开后要关闭
            return true;
        }

        //获得所有的题目
        bool GetAllQuestions(vector<Question> *allquestion)
        {
            if (questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败" << endl;
                return false;
            }
            for (auto &q : questions)
            {
                (*allquestion).push_back(q.second);
            }
            
            return true;
        }
        //获得单个题目
        bool GetOneQuestions(string num, Question *onequestion)
        {
            auto iter = questions.find(num);
            if (iter == questions.end())
            {
                LOG(ERROR)<<"用户获取题目失败: "<<num<<endl;
                return false;
            }
            (*onequestion) = iter->second;
        }
  
        ~Model()
        {
        }
    };
}